package archer.common.utils

import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, FileInputStream }
import java.nio.file.{ Path, Paths }
import java.util.UUID
import javax.imageio.ImageIO

import net.coobird.thumbnailator.Thumbnails
import org.apache.commons.io.IOUtils

import scala.util.{ Properties, Try }

object PictureUtils {

  val tempDir = Properties.tmpDir
  val picturePath = Paths.get(tempDir).resolve("kunlun/student/pictures")
  val tempPicturePath =
    Paths.get(tempDir).resolve("kunlun/student/temp-pictures")
  val pdfParentPath = Paths.get(tempDir).resolve("kunlun/student/pdf")

  val excelPath = Paths.get(tempDir).resolve("kunlun/back/excel")

  def getFileNameById(id: Int, maxWidth: Int, maxHeight: Int): String =
    s"${id}_${maxWidth}_${maxHeight}.jpg"
  def getPathById(id: Int, maxWidth: Int, maxHeight: Int): Path =
    picturePath.resolve(getFileNameById(id, maxWidth, maxHeight))
  def getTempPicturePathById(id: Int, maxWidth: Int, maxHeight: Int): Path =
    tempPicturePath.resolve(getFileNameById(id, maxWidth, maxHeight))

  def resizePic(
    content: Array[Byte],
    maxWidth: Int,
    maxHeight: Int): Array[Byte] = {
    val inputPictureStream = new ByteArrayInputStream(content)
    val thumbnailsInputPictureStream = new ByteArrayInputStream(content)
    val outPutStream = new ByteArrayOutputStream()

    try {
      val imgIO = ImageIO.read(inputPictureStream)
      val width = imgIO.getWidth
      val height = imgIO.getHeight
      val scale = 1

      val thumbnails = Thumbnails.of(thumbnailsInputPictureStream)

      val fixThumb = if (maxWidth > 0 && maxHeight > 0) {
        val virtualHeight = maxWidth * scale * height / width
        if (virtualHeight > maxHeight * scale) {
          thumbnails.width(maxHeight * scale * width / height)
        } else {
          thumbnails.height(virtualHeight)
        }
      } else if (maxWidth < 0 && maxHeight > 0) {
        thumbnails.height(maxHeight)
      } else if (maxWidth > 0 && maxHeight < 0) {
        thumbnails.height(maxWidth)
      } else {
        thumbnails.width(width).height(height)
      }

      fixThumb.outputFormat("jpg").toOutputStream(outPutStream)
      outPutStream.toByteArray
    } finally {
      Try {
        inputPictureStream.close()
      }
      Try {
        thumbnailsInputPictureStream.close()
      }
      Try {
        outPutStream.close()
      }
    }
  }

  def randomPdfPath: Path = {
    pdfParentPath.resolve(UUID.randomUUID().toString)
  }

  val defaultWidthPx = 95
  val defaultHeightPx = 132

  def genPicBytes(path: Path): Array[Byte] = {
    val inputPictureStream = new FileInputStream(path.toFile)
    val outPutStream = new ByteArrayOutputStream()
    val scale = 3

    try {
      val imgIO = ImageIO.read(inputPictureStream)
      val width = imgIO.getWidth
      val height = imgIO.getHeight

      val thumbnails = Thumbnails.of(path.toFile)

      val virtualHeight = defaultWidthPx * scale * height / width
      val fixThumb =
        if (height < (defaultHeightPx * 3) && width < (defaultWidthPx * 3)) {
          thumbnails.width(width).height(height)
        } else if (virtualHeight > defaultHeightPx * scale) {
          thumbnails.width(defaultHeightPx * scale * width / height)
        } else {
          thumbnails.height(virtualHeight)
        }

      fixThumb.outputFormat("jpg").toOutputStream(outPutStream)
      outPutStream.toByteArray
    } finally {
      Try {
        inputPictureStream.close()
      }
      Try {
        outPutStream.close()
      }
    }
  }

  def genExcelPath = excelPath.resolve(UUID.randomUUID().toString)

}
